<?php
/**
 * 现金卡 ajax类
 * @package  ajax
 * @subpackage  company
 * @author   create by allen.qiang
 */

quanli_require('module/base/company/cardMaster.class.php');
quanli_require('module/base/company/cardDetail.class.php');

class ajax_card{

    public function execute() {

        if(isset($_REQUEST['operFlg']) && quanli_is_int($_REQUEST['operFlg'])){

            switch($_REQUEST['operFlg']){

                case 1://现金卡列表
                    $this->cardList();
                    break;

                case 2://查询代金卡
                    $this->searchCard();
                    break;

                case 3: //审核现金卡生产单
                    $this->auditCard();
                    break;

//                case 4://现金卡详情列表
//                    $this->cardDetailList();
//                    break;

                case 5://检查现金卡卡号
                    $this->checkCardNo();
                    break;

                case 6://现金卡入库
                    $this->warehouseCard();
                    break;

                case 7://生成现金卡
                    $this->createCode();
                    break;

                case 8://查看现金卡编号
                    $this->companyCodeList();
                    break;

                case 9://现金卡调拨
                    $this->cardAllot();
                    break;

                case 10://检查现金卡号调拨
                    $this->checkCardNoAlloat();
                    break;

                //add by 2014/11/04 sunqiang FUN-BK-0011 现金卡密码查询与重置 start
                case 11://查询现金卡密
                    $this->getCardInfo();
                    break;
                case 12://现金卡重置
                    $this->resetCard();
                    break;
                case 13://查询现金卡使用过的订单、退货单
                    $this->getOrderArrayByUseCard();
                    break;
                //add by 2014/11/04 sunqiang FUN-BK-0011 现金卡密码查询与重置 end

                case 100:
                    if (!isset($_REQUEST['act']) || !in_array($_REQUEST['act'], array('del', 'edit'))) {

                        returnJsonData('参数错误');

                    }

                    if ($_REQUEST['act'] == 'edit') {

                        $this->edit();//新增、修改 现金卡

                    } else if ($_REQUEST['act'] == 'del') {
                        $this->deleteCard();//删除现金卡
                    }

                    break;

                default:

                    returnJsonData('参数错误');

                    break;
            }

        }else {
            returnJsonData('参数错误');
        }
    }

    /* 使用过的订单
     * FUN-BK-0011 现金卡密码查询与重置
     * @package ajax
     * @subpageage backend
     * @author sunqiang<allen.qiang@fistforward.com>
     * */
    private function getOrderArrayByUseCard()
    {
        global $G_PGLOB;

        $card_no = $_REQUEST['card_no'];

        $sql = ' SELECT bm.bill_no , bm.bill_code , bm.bill_status , GROUP_CONCAT(br.return_no) AS return_no , GROUP_CONCAT( br.return_status) AS return_status FROM bill_use_card buc
                        LEFT JOIN bill_master bm ON bm.bill_no = buc.bill_no
                        LEFT JOIN bill_return br ON buc.bill_code = br.bill_code
                        WHERE buc.card_no = ?
                        GROUP BY bm.bill_code
                        ORDER BY bm.createTime DESC';

        $card_array = $G_PGLOB->DBCA->stmt_query($sql,'i', array( $card_no ) );


        $result_array = array( //组织返回的数据
            'page' => 1,
            'total' => 0,
            'rows' => array()
        );
        if($card_array)
        {
            $result_array['total'] = count($result_array);
            foreach($card_array as $card)
            {

                $btn_bill_code = '<a href="/quanliadmin/app/bill/billSearch.pscript?operFlg=9&bill_no=' . $card->bill_no . '" target="_blank">' . $card->bill_code . '</a>';
                $btn_return_no = array();
                $btn_return_status = array();
                $return_no_array = explode(',',$card->return_no);
                $return_status_array = explode(',',$card->return_status);
                foreach($return_no_array as $no)
                {
                    if(isset($no) && !empty($no))
                    {
                        $btn_return_no[] = "<a href='javascript: void(0);' onclick='card.return_view.open(".$no.", 0);'>".$no."</a>&nbsp;";
                    }
                }
                foreach($return_status_array as $status)
                {
                    if ($status == 1) {
                        $btn_return_status[] = '未审核';
                    } else if ($status == 2) {
                        $btn_return_status[] = '已审核';
                    } else if ($status == 3) {
                        $btn_return_status[] = '已收货';
                    } else if ($status == 4) {
                        $btn_return_status[] = '已取消';
                    }

                }


                $result_array['rows'][] = array (
                    'id' => $card->bill_code,
                    'cell' => array(
                        $btn_bill_code,
                        $GLOBALS['backend_bill_status'][$card->bill_status],
                        implode('<br />',$btn_return_no),
                        implode('<br />',$btn_return_status)
                    )
                );

            }

        }

        echo json_encode($result_array,true);

    }

    /* 重置现金卡
     * FUN-BK-0011 现金卡密码查询与重置
     * @package ajax
     * @subpageage backend
     * @author sunqiang<allen.qiang@fistforward.com>
     * */
    private function resetCard(){
        global $G_PGLOB;
        $reset_permission = $G_PGLOB->has_permission(70015);//重置权限
        if( ! $reset_permission )
        {
            returnJsonData('权限不足');
        }


        if(!isset($_REQUEST['card_no']) || empty($_REQUEST['card_no']))
        {
            returnJsonData('现金卡号不存在,请重试');
        }
        if(is_int($_REQUEST['card_no']))
        {
            returnJsonData('现金卡号不正确,请重试');
        }

        $card_no = $_REQUEST['card_no']; //现金卡号//非空，数字


        $card_info = new entity('card_detail');
        $card_info->card_no = $card_no;
        $G_PGLOB->DBCA->fetch($card_info);

        if(!$card_info)
        {
            returnJsonData('现金卡不存在,请重试');
        }


        $result_msg = '';
        $flag = false;
        if( in_array($card_info->card_status,array(1,2,3,4)))
        {
            $result_msg = "现金卡 $card_no 还未到'已发卡'状态,不可重置";
        }
        else if( in_array($card_info->card_status,array(5)))
        {
            $result_msg = "现金卡 $card_no 为'已发卡'状态,无需重置";
        }
        else if( in_array($card_info->card_status,array(6,7)))
        {
            $flag = true;
            $card_info->card_status = 5;
            $card_info->bill_no = null;
            $card_info->resetCount =  $card_info->resetCount+1;
            $G_PGLOB->DBCA->save($card_info);
        }


        //记录重置日志
        $log_msg = ( "现金卡重置" . ($flag?"成功":"失败：")  . $result_msg);
        $log_msg .= ( "<br />操作人：" . $G_PGLOB->currentUserID ) ;
        $log_msg .= ( "<br />授权人：" . $_REQUEST['authorization_user'] ) ;
        $log_msg .= ( "<br />重置成功次数：" . ( empty($card_info->resetCount) ? 0 : $card_info->resetCount ) );

        $this->save_card_log($card_no,'现金卡重置',$log_msg  );


        if(!$flag)
        {
            returnJsonData($result_msg);
        }
        else
        {
            returnJsonData('ok',200);
        }
    }

    /* 查询现金卡密
     * FUN-BK-0011 现金卡密码查询与重置
     * @package ajax
     * @subpageage backend
     * @author sunqiang<allen.qiang@fistforward.com>
     * */
    private function getCardInfo (){
        global $G_PGLOB;


        $card_no = $_REQUEST['card_no']; //现金卡号//非空，数字



        $cardDetail = new cardDetail( $G_PGLOB->DBCA );//实例化代金卡编号类

        $result_array = array( //组织返回的数据
            'page' => 1,
            'total' => 0,
            'rows' => array()
        );

        $arg_array = array(
            'card_no' => $card_no
        );
        $card_array = $cardDetail->searchCardDetail($arg_array);


        if( $card_array )
        {
            $card_array = $card_array[0];
            $result_array['total'] = 1;

            $reset_permission = $G_PGLOB->has_permission(70015);//重置权限
            $operation_html = ''; //重置按钮
            if( $reset_permission )
            {
                //$operation_html = '<a href="javascript:void(0);" onclick="card.reset(\"" "\",\"\");">重置现金卡</a>&nbsp;&nbsp;';
                $operation_html = '<a href="javascript:void(0);" onclick="card.reset(' .$card_no . ','  . $card_array['card_status'] . ');">重置现金卡</a>&nbsp;&nbsp;';
            }


            $card_type="";
            if( $card_array['card_type'] == 1 ){
                $card_type = '现金卡';
            }elseif ( $card_array['card_type'] == 2 ){
                $card_type = '专享卡';
            }elseif ( $card_array['card_type']== 3 ){
                $card_type = '其他';
            }

            $card_status ="";
            if($card_array['card_status'] == 1){
                $card_status = "已生产";
            }else if ($card_array['card_status'] == 2){
                $card_status = "入库申请";
            }else if ($card_array['card_status'] == 3){
                $card_status = "已入库";
            }else if ($card_array['card_status'] == 4){
                $card_status = "申领申请";
            }else if ($card_array['card_status'] == 5){
                $card_status = "已发卡";
            }else if ($card_array['card_status'] == 6){
                $card_status = "已使用";
            }else if ($card_array['card_status'] == 7){
                $card_status = "作废";
            }
            $result_array['rows'][] = array (
                'id' => $card_array['card_no'],
                'cell' => array(
                    $operation_html, //操作
                    $card_array['card_no'],                  //现金卡卡号
                    decryptD($card_array['card_pwd']),                  //现金卡密码
                    $card_type,                  //现金卡类型
                    $card_array['face_value'],                  //现金卡面值
                    $card_status,                  //现金卡状态
                    $card_array['can_use_number'],                  //可使用次数
                    $card_array['used_number'],                  //已使用次数
                    $card_array['startDate'],                  //有效期开始日期
                    $card_array['endDate']                  //有效期结束日期
                )
            );


        }

        //更新查询次数
        $card_detail = new entity('card_detail');
        $card_detail->card_no = $card_no;
        $G_PGLOB->DBCA->fetch($card_detail);
        if($card_detail)
        {
            $card_detail->searchCount = $card_detail->searchCount + 1;
            $G_PGLOB->DBCA->save($card_detail);


            //记录查询日志
            $log_msg = "现金卡查询";
            $log_msg .= ( "<br />操作人：" . $G_PGLOB->currentUserID ) ;
            $log_msg .= ( "<br />授权人：" . $_REQUEST['authorization_user'] ) ;
            $log_msg .= ( "<br />查询次数：" . $card_detail->searchCount );

            $this->save_card_log($card_no,'现金卡查询',$log_msg  );
        }





        echo json_encode($result_array);
    }

    private function save_card_log($card_no , $type_msg ,$detail_msg )
    {
        global $G_PGLOB;

        //保存日志信息
        $log = new entity('log_detail');
        $log->creator = $G_PGLOB->currentUserName;
        $log->logCategoryID = 13;
        $log->logItem = $card_no;
        $log->extend_1  = $type_msg;
        $log->extend_2  = $detail_msg;
        $G_PGLOB->DBCA->save($log);
    }


    /**
     * 现金卡列表
     */
    private function cardList(){

        global $G_PGLOB;

        //实例化现金卡类
        $cardMaster = new cardMaster();
        $arg_array = $_REQUEST;
        $result = $cardMaster->search( $arg_array );

        //查询卡详情
        $sql = "SELECT COUNT(*) AS num,card_id FROM card_detail GROUP BY card_id";
        $card_num_array = $G_PGLOB->DBCA->stmt_query($sql);

        if($result){
            foreach($result as $key=> $value){

                $sql = "SELECT cityName FROM card_city WHERE card_id = " . $value['card_id'];
                $city_name_array = $G_PGLOB->DBCA->stmt_query($sql);
                $result[$key]['citys'] = $city_name_array;
            }
        }

        foreach($result as &$data){

            $city_array = array();
            foreach($data['citys'] as &$city){
                  $city_array[] = $city->cityName;
            }
            $data['citys'] = implode("," , $city_array);
        }

        //组织返回的数据
        $result_array = array(
            'page' => $arg_array['page'],
            'total' => $arg_array['count'],
            'rows' => array()
        );

        if ($result) {

            $edit_permission = $G_PGLOB->has_permission(70001);


            foreach($result as &$data) {

                $card_type="";
                if( $data['card_type'] == 1 ){
                    $card_type = '现金卡';
                }elseif ( $data['card_type'] == 2 ){
                    $card_type = '专享卡';
                }elseif ( $data['card_type']== 3 ){
                    $card_type = '其他';
                }

                $card_status ="";
                if($data['card_status'] == 1){
                    $card_status = "待审核";
                }else if ($data['card_status'] == 2){
                    $card_status = "已审核";
                }else{
                    $card_status = "已生产";
                }

                $card_into = "";
                if ($data['card_status'] == 4){
                    $card_into = "已入库";
                }else{
                    $card_into = "未入库";
                }
                $operation_html = '';
                if ($edit_permission) {



                    if($data['card_status'] == 1){
                        $operation_html .= '<a href="javascript:void(0);" onclick="cardList.audit(' . $data['card_id'] . ');">审核</a>&nbsp;&nbsp;';
                        $operation_html .= '<a href="javascript:void(0);" onclick="cardList.edit.open(' . $data['card_id'] . ');">修改</a>&nbsp;&nbsp;';

                        $operation_html .= '<a href="javascript:void(0);" onclick="cardList.del.check(' . $data['card_id'] . ');">删除</a>';
                    } else if($data['card_status'] == 2){
                        $operation_html .= '<a href="javascript:void(0);" onclick="cardList.add(' . $data['card_id'] . ');">生产</a>';

                    } else if ($data['card_status'] == 3){



                        $operation_html .= '<a href="javascript: void(0);" onclick="cardList.putinStore(' . $data['card_id'] . ')">入库</a>&nbsp;&nbsp;';

                        if ( !empty( $data['card_warehouse_voucher_number'] ) && $data['card_warehouse_voucher_number'] != $data['card_product_number']  ) {

                            $operation_html .= '<a href="javascript: void(0);" onclick="cardList.allot(' . $data['card_id'] . ')">调拨</a>&nbsp;&nbsp;';

                            $card_into = "部分入库";
                        }

                        $operation_html .= '<a href="javascript: void(0);" onclick="cardList.inquiredNumber(' . $data['card_id'] . ')">编号查询</a>&nbsp;&nbsp;';

                        foreach($card_num_array as $card_num){
                            if($card_num->card_id == $data['card_id']){
                                $operation_html .= '<a href="javascript: void(0);" onclick="cardList.exportCard(' . $data['card_id'] . ',' . $card_num->num . ')">导出</a>';
                            }
                        }

                    } else if($data['card_status'] == 4){

                        $operation_html .= '<a href="javascript: void(0);" onclick="cardList.allot(' . $data['card_id'] . ')">调拨</a>&nbsp;&nbsp;';

                        $operation_html .= '<a href="javascript: void(0);" onclick="cardList.inquiredNumber(' . $data['card_id'] . ')">编号查询</a>&nbsp;&nbsp;';

                        foreach($card_num_array as $card_num){
                            if($card_num->card_id == $data['card_id']){
                                $operation_html .= '<a href="javascript: void(0);" onclick="cardList.exportCard(' . $data['card_id'] . ',' . $card_num->num . ')">导出</a>';
                            }
                        }
                    }

                    $result_array['rows'][] = array (
                        'id' => $data['card_id'],
                        'cell' => array(
                            $operation_html,
                            $data['card_id'],
                            $data['card_name'],
                            $data['face_value'],
                            $data['card_product_number'],
                            $data['can_use_number'],
                            $card_status,
                            $card_into,
                            $data['startDate'],
                            $data['endDate'],
                            $card_type,
                            $data['card_spec'],
                            $data['citys'],
                            $data['selectName'],
                            $data['remark']


                        )
                    );

                } else {

                    $operation_html = '';
                    $result_array['rows'][] = array (
                        'id' => $data['card_id'],
                        'cell' => array(
                            $operation_html,
                            $data['card_id'],
                            $data['card_name'],
                            $data['face_value'],
                            $data['card_product_number'],
                            $data['can_use_number'],
                            $card_status,
                            $card_into,
                            $data['startDate'],
                            $data['endDate'],
                            $card_type,
                            $data['card_spec'],
                            $data['citys'],
                            $data['selectName'],
                            $data['remark']
                        )
                    );

                }

            }

        }


        echo json_encode($result_array);
    }

    /**
     * 新增/修改  现金卡
     */
    private function edit() {

        global $G_SHOP;

        //实例现金卡类
        $cardMaster = new cardMaster();

        //新增/修改
        $arg_array = $_REQUEST;
        $arg_array['creator'] = $G_SHOP->currentUserID;
        $arg_array['card_name'] = $arg_array['save_card_name'];
        $arg_array['startDate'] = $arg_array['save_startDate'];
        $arg_array['endDate'] = $arg_array['save_endDate'];
        $arg_array['card_type'] = $arg_array['save_card_type'];
        $result_array = $cardMaster->editCard( $arg_array );
        returnJsonData($result_array['message'], $result_array['code']);

    }

    /**
     * 查询现金卡方式
     */
    private function searchCard() {

        global $G_SHOP;

        if (!isset($_REQUEST['card_id']) || !quanli_is_int($_REQUEST['card_id'])) {
            returnJsonData('参数错误');
        }

        //实例现金卡类
        $cardMaster = new cardMaster();

        //查询现金卡方式
        $arg_array = array(
            'card_id' => $_REQUEST['card_id']
        );
        $card_array = $cardMaster->search( $arg_array );

        $sql = "SELECT cityID,cityName FROM card_city WHERE card_id = ?";
        $city_array = $G_SHOP->DBCA->stmt_query($sql,"i",$_REQUEST['card_id']);

        foreach($city_array as $key => $city){
            $card_array[0]['city'][$key]['cityID'] = $city->cityID;
            $card_array[0]['city'][$key]['cityName'] = $city->cityName;
        }

        if ($card_array) {
            returnJsonData('ok', 200, $card_array[0]);
        } else {
            returnJsonData('现金卡信息没有找到', 404);
        }

    }

    /**
     * 删除现金卡
     */
    private function deleteCard () {

        //实例现金卡类
        $cardMaster = new cardMaster();

        //删除现金卡
        $result_array = $cardMaster->deleteCard( $_REQUEST );

        returnJsonData( $result_array['message'], $result_array['code'] );

    }


    //审核现金卡
    public function auditCard(){

        global $G_PGLOB;

        //调用卡管理类中的审核现金卡生产单方法
        $cardMaster = new cardMaster( $G_PGLOB->DBCA ) ;

        $result_array = $cardMaster->auditCardProduct( $_REQUEST['card_id'] ) ;

        echo json_encode($result_array);

    }

    /**
     * 生成现金卡
     */
    private function createCode () {

        //实例现金卡类
        $cardMaster = new cardMaster();

        //代金卡生产
        $result_array = $cardMaster->createCardCode( $_REQUEST['card_id'] );

        echo json_encode( $result_array );

    }


    /**
     * 查看现金卡编号
     */
    private function companyCodeList() {

        global $G_PGLOB;

        //实例化代金卡编号类
        $cardDetail = new cardDetail( $G_PGLOB->DBCA );
 
        //查询
        $company_code_array = $cardDetail->searchCardDetail( $_REQUEST );

        //返回数组
        $result_array = array(
            'page' => $_REQUEST['page'],
            'total' => $_REQUEST['count'],
            'rows' => array()
        );

        if ($company_code_array){
            foreach ($company_code_array as  $company_code) {

                $card_type="";
                if( $company_code['card_type'] == 1 ){
                    $card_type = '现金卡';
                }elseif ( $company_code['card_type'] == 2 ){
                    $card_type = '专享卡';
                }elseif ( $company_code['card_type']== 3 ){
                    $card_type = '其他';
                }

                $card_status ="";
                if($company_code['card_status'] == 1){
                    $card_status = "已生产";
                }else if ($company_code['card_status'] == 2){
                    $card_status = "入库申请";
                }else if ($company_code['card_status'] == 3){
                    $card_status = "已入库";
                }else if ($company_code['card_status'] == 4){
                    $card_status = "申领申请";
                }else if ($company_code['card_status'] == 5){
                    $card_status = "已发卡";
                }else if ($company_code['card_status'] == 6){
                    $card_status = "已使用";
                }else if ($company_code['card_status'] == 7){
                    $card_status = "作废";
                }


                if( isset($_REQUEST['card_id']) ){
                    $result_array['rows'][] = array(
                        'id' => $company_code['card_no'],
                        'cell' => array(
                            $company_code['card_no'],
                            $card_type,
                            $company_code['face_value'],
                            $card_status,
                            $company_code['can_use_number'],
                            $company_code['used_number'],
                            $company_code['bill_no'],
                            $company_code['startDate'],
                            $company_code['endDate']
                        )
                    );

                }
            }
        }

        echo json_encode( $result_array );


    }

    /**
     * 现金卡入库，检查卡号
     */
    private function checkCardNo() {

        if (!isset($_REQUEST['card_id'], $_REQUEST['card_no_start'], $_REQUEST['card_no_end'])) {
            returnJsonData('参数错误');
        }

        if (!quanli_is_int($_REQUEST['card_id']) || !quanli_is_int($_REQUEST['card_no_start']) || !quanli_is_int($_REQUEST['card_no_end'])) {
            returnJsonData('参数错误');
        }

        quanli_include('module/base/company/cardDetail.class.php');

        //实例金卡详情类
        $cardDetail = new cardDetail();

        //实例现金卡类
        $cardMaster = new cardMaster() ;

        //查询代金卡详情
        $args_array = array(
            'extend' => sprintf(' AND card_no BETWEEN %s AND %s', $_REQUEST['card_no_start'], $_REQUEST['card_no_end'])
        );
        $card_detail_array = $cardDetail->searchCardDetail( $args_array );
        if (!$card_detail_array) {
            returnJsonData('现金卡信息没有找到', 404);
        }

        //现金卡数量
        $card_num = 0;

        //循环代金卡，检查代金卡状态
        foreach($card_detail_array as &$card_detail) {
            if ($card_detail['card_status'] == 2) {
                returnJsonData(sprintf('现金卡：%s，入库申请', $card_detail['card_no']), 404);
            }
            if ($card_detail['card_status'] == 3) {
                returnJsonData(sprintf('现金卡：%s，已入库', $card_detail['card_no']), 404);
            }

            $card_num++;
        }

        //查询现金卡详情
        $args_array = array(
            'card_id' => $_REQUEST['card_id']
        );
        $apply_card_detail_array = $cardMaster->search( $args_array );
        if (!$apply_card_detail_array) {
            returnJsonData('现金卡入库信息没有找到', 404);
        }

        //循环现金卡，检查录入的现金卡是否匹配
        foreach($apply_card_detail_array as &$apply_card_detail) {
            if ($apply_card_detail['face_value'] != $card_detail_array[0]['face_value']) {
                continue;
            }
            if ($apply_card_detail['card_product_number'] < $card_num) {
                continue;
            }
            $return_array = array(
                'card_product_number' => $card_num,
                'face_value' => $apply_card_detail['face_value'],
                'card_spec' => $apply_card_detail['card_spec']
            );
            break;
        }

        if (empty($return_array)) {
            returnJsonData('录入的现金卡与现金卡不匹配', 404);
        }

        returnJsonData('ok', 200, $return_array);

    }


    //现金卡入库
    private function warehouseCard(){

        global $G_PGLOB;

        $_REQUEST['operator'] = $G_PGLOB->currentUserID;

        //实例化现金卡类
        $cardMaster = new cardMaster() ;

        $result = $cardMaster->warehouseCard($_REQUEST);

        returnJsonData($result['message'], $result['code'] );

    }

    /**
     * 现金卡调拨，检查卡号
     */
    private function checkCardNoAlloat() {

        if (!isset($_REQUEST['card_id'], $_REQUEST['card_no_start'], $_REQUEST['card_no_end'])) {
            returnJsonData('参数错误');
        }

        if (!quanli_is_int($_REQUEST['card_id']) || !quanli_is_int($_REQUEST['card_no_start']) || !quanli_is_int($_REQUEST['card_no_end'])) {
            returnJsonData('参数错误');
        }

        quanli_include('module/base/company/cardDetail.class.php');

        //实例金卡详情类
        $cardDetail = new cardDetail();

        //实例现金卡类
        $cardMaster = new cardMaster() ;

        //查询代金卡详情
        $args_array = array(
            'extend' => sprintf(' AND card_no BETWEEN %s AND %s', $_REQUEST['card_no_start'], $_REQUEST['card_no_end'])
        );
        $card_detail_array = $cardDetail->searchCardDetail( $args_array );
        if (!$card_detail_array) {
            returnJsonData('现金卡信息没有找到', 404);
        }

        //现金卡数量
        $card_num = 0;

        //循环代金卡，检查代金卡状态
        foreach($card_detail_array as &$card_detail) {
            if ($card_detail['card_status'] == 1) {
                returnJsonData(sprintf('现金卡：%s，未入库', $card_detail['card_no']), 404);
            }

            if ($card_detail['card_status'] == 2) {
                returnJsonData(sprintf('现金卡：%s，请入库申请', $card_detail['card_no']), 404);
            }
//            if ($card_detail['card_status'] == 3) {
//                returnJsonData(sprintf('现金卡：%s，已入库', $card_detail['card_no']), 404);
//            }

            $card_num++;
        }

        //查询现金卡详情
        $args_array = array(
            'card_id' => $_REQUEST['card_id']
        );
        $apply_card_detail_array = $cardMaster->search( $args_array );
        if (!$apply_card_detail_array) {
            returnJsonData('现金卡入库信息没有找到', 404);
        }

        //循环现金卡，检查录入的现金卡是否匹配
        foreach($apply_card_detail_array as &$apply_card_detail) {
            if ($apply_card_detail['card_id'] != $card_detail_array[0]['card_id']) {
                continue;
            }
            if ($apply_card_detail['card_product_number'] < $card_num) {
                continue;
            }
            $return_array = array(
                'card_product_number' => $card_num,
                'face_value' => $apply_card_detail['face_value'],
                'card_id' => $apply_card_detail['card_id'],
                'card_spec' => $apply_card_detail['card_spec']
            );
            break;
        }

        if (empty($return_array)) {
            returnJsonData('录入的现金卡与现金卡不匹配', 404);
        }

        returnJsonData('ok', 200, $return_array);

    }


    //现金卡调拨
    private function cardAllot(){

        global $G_PGLOB;

        $_REQUEST['operator'] = $G_PGLOB->currentUserID;

        //实例化现金卡类
        $cardMaster = new cardMaster() ;

        //判断现金卡是否已经调拨
        $arg_array = array(
            "extend_sql" => " AND  office_id = " . $_REQUEST["card_detail"][1]["office_id"] . "	AND ( card_no_start <= " . $_REQUEST["card_detail"][1]["card_no_start"] . " AND card_no_end >= " . $_REQUEST["card_detail"][1]["card_no_start"] . ")
                            OR (card_no_start <= " . $_REQUEST["card_detail"][1]["card_no_end"] . " AND card_no_end >= " . $_REQUEST["card_detail"][1]["card_no_end"] .")"
        );


        $res_temp_array = $cardMaster->searchCardAllot($arg_array);



        if ( $res_temp_array ) {
            returnJsonData("已经调拨过", 400 );
        }

        $result = $cardMaster->cardAllot($_REQUEST);

        returnJsonData($result['message'], $result['code'] );

    }

}
